__author__ = "ncll"
__data__ = "2020/12/1 8:59"


import pymysql

db = pymysql.connect("192.168.6.227", "chenl", "chenl", "chenl", 3306)
cur = db.cursor()
num = cur.execute("select stuNum from studentscl where id='1'")
print(str(cur.fetchone()))
print(type(cur.fetchone()))
db.close()


# # 一、创建表studengtscl
# import pymysql
#
# # 创建connection
# db = pymysql.connect("192.168.6.227", "chenl", "chenl", "chenl", 3306)
# # 获取游标
# cursor = db.cursor()
#
# cursor.execute("drop table if EXISTS studengtscl")
# sql = """create table studentscl(
#    id int unsigned primary key auto_increment,
#    name varchar(10),
#      sex varchar(10),
#    age int unsigned,
#    height decimal(5,2))"""
# try:
#     cursor.execute(sql)
#     print("数据表创建成功")
# except Exception as res:
#     print("数据表创建失败： %s" % res)
#
# finally:
#     db.close()

# 二、往表中插入数据
# 开始事务：begin   提交事务：commit   回滚事务：rollback

# import pymysql
#
# db = pymysql.connect("192.168.6.227", "chenl", "chenl", "chenl", 3306)
#
# cursor = db.cursor()
#
# sql = '''insert into studentscl values
# ('001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),
# ('002', '诸葛亮', '男', '上海', '18', '2班', '340322199002242354'),
# ('003', '张飞', '男', '南京', '24', '3班', '340322199003247654'),
# ('004', '白起', '男', '安徽', '22', '4班', '340322199005247654'),
# ('005', '大乔', '女', '天津', '19', '3班', '340322199004247654'),
# ('006', '孙尚香', '女', '河北', '18', '1班', '340322199006247654'),
# ('007', '百里玄策', '男', '山西', '20', '2班', '340322199007247654'),
# ('008', '小乔', '女', '河南', '15', '3班', null),
# ('009', '百里守约', '男', '湖南', '21', '1班', ''),
# ('010', '妲己', '女', '广东', '26', '2班', '340322199607247654'),
# ('011', '李白', '男', '北京', '30', '4班', '340322199005267754'),
# ('012', '孙膑', '男', '新疆', '26', '3班', '340322199000297655')
# '''
# try:
#     cursor.execute(sql)
#     db.commit()
#     print("插入数据成功")
# except Exception as res:
#     print("插入数据失败： %s" % res)
# finally:
#     db.close()

# 三、删除数据

# import pymysql
#
# db = pymysql.connect("192.168.6.227", "chenl", "chenl", "chenl", 3306)
# cursor = db.cursor()
# sql = "delete from studentscl where id = 15"
# try:
#     cursor.execute(sql)
#     db.commit()
#     print("删除成功")
# except Exception as res:
#     print("删除失败： %s" % res)
# finally:
#     db.close()

# 四、修改数据

# import pymysql
#
# db = pymysql.connect("192.168.6.227", "chenl", "chenl", "chenl", 3306)
# cursor = db.cursor()
# sql = "update studentscl set name = '张飞飞' where name ='张飞'"
# try:
#     cursor.execute(sql)
#     db.commit()
#     print("更新数据成功")
# except Exception as res:
#     print("更新数据失败: %s" % res)
# finally:
#     db.close()

# 五、打印表中数据
# fetchone:取出一条数据 可结合循环便利使用
# fetchmany(count)：取出多条数据，count表示要取出数据的条数（例如3，是取出3条数据），count可以超出数据的条数
# fetchall：取出所有的数据
# 以上方法返回的都是元祖 tuple

# import pymysql
#
# db = pymysql.connect("192.168.6.227", "chenl", "chenl", "chenl", 3306)
# cursor = db.cursor()
# sql = "select * from studentscl where id>=4"
# # count 为执行sql语句后，返回的受影响的行数
# count = cursor.execute(sql)
# for i in range(count):
#     print(cursor.fetchall())

# import random
#
#
# def game_cq(a, b, win_count, lose_count, i=1):
#
#     if a == 1:
#         if (b == 2):
#             print("第%d局游戏:你赢了" % i)
#             win_count += 1
#             print("一共赢了%d局" % win_count)
#         if (b == 0):
#             print("第%d局游戏:你输了" % i)
#             lose_count += 1
#             print("一共输了%d局" % lose_count)
#     if a == 0:
#         if (b == 1):
#             print("第%d局游戏:你赢了" % i)
#             win_count += 1
#             print("一共赢了%d局" % win_count)
#         if (b == 2):
#             print("第%d局游戏:你输了" % i)
#             lose_count += 1
#             print("一共输了%d局" % lose_count)
#     if a == 2:
#         if b == 0:
#             print("第%d局游戏:你赢了" % i)
#             win_count += 1
#             print("一共赢了%d局" % win_count)
#         if b == 1:
#             print("第%d局游戏:你输了" % i)
#             lose_count += 1
#             print("一共输了%d局" % lose_count)
#     return [win_count, lose_count]
#
#
# count = 1
# win_count = 0
# lose_count = 0
# for i in range(100):
#     while 1:
#         try:
#             player = eval(input("请输入：石头（0），剪刀（1），布（2）"))
#             if player not in [0, 1, 2]:
#                 print("请输入0、1、2中的一个数")
#             else:
#                 break
#         except Exception as res:
#             print(res)
#     computer = random.randint(0, 2)  # 电脑随机输入（石头（0），剪刀（1），布（2））
#     print("石头（0），剪刀（1），布（2）,电脑出的是 %d" % computer)
#     if player == computer:
#         print("平局，继续")
#         continue
#     list1 = game_cq(player, computer, win_count, lose_count, count)
#
#     win_count = list1[0]
#     # print("赢%d" % win_count)
#     lose_count = list1[1]
#     # print("输%d" % lose_count)
#     if win_count == 2:
#         print("\n三局两胜的规则你已获胜两局：游戏结束")
#         break
#     if lose_count == 2:
#         print("\n三局两胜的规则你已输了两局：游戏结束")
#         break
#     print("\n第%d局游戏开始：" % (count + 1))
#     count += 1



